﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.AdminWeb.DtoModels;
using MvcMall.Core.Contacts.Tasks;

namespace MvcMall.AdminWeb.Manage.GoodsCategories
{
    public partial class Index : ManageBasePage
    {
        private IGoodsCategoryTask _goodsCategoryTask;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("GoodsCategoriesIndex");

            _goodsCategoryTask = GetInstance<IGoodsCategoryTask>();

            if (!IsPostBack)
            {
                BindRpt();
            }
        }

        private void BindRpt()
        {
            ANPager.Attributes["Name"] = txtSearchCatName.Text.Trim();
            ANPager.Attributes["CatGrade"] = ddlGrades.SelectedValue;
            var searchName = txtSearchCatName.Text.Trim();
            var grade = string.IsNullOrEmpty(ddlGrades.SelectedValue)
                            ? (int?)null
                            : Convert.ToInt32(ddlGrades.SelectedValue);

            var list = _goodsCategoryTask.GetPagedList(ANPager.CurrentPageIndex, ANPager.PageSize, searchName, grade);
            var dtoList = list.Select(c => new GoodsCategoryDto
                                               {
                                                   CatDesc = c.CatDesc,
                                                   CatGrade = c.CatGrade,
                                                   CatName = c.CatName,
                                                   GoodsNumber = c.GoodsNumber,
                                                   Id = c.Id,
                                                   Keywords = c.Keywords,
                                                   ParentCatName = c.ParentCat.CatName,
                                                   SortOrder = c.SortOrder
                                               });
            rptRoles.DataSource = dtoList;
            rptRoles.DataBind();
            ANPager.RecordCount = list.TotalItemCount;
        }

        protected void ANPager_PageChanged(object sender, EventArgs e)
        {
            BindRpt();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindRpt();
        }
    }
}